@layout("/common/_curd/_container.html"){

<form data-url="${basePath}/${code}/update" method="post" class="form-horizontal" role="form">
    <input type="hidden" name="tb_auth_tag.teacher_auth" id="_teacher_auth" value="${item.teacher_auth}"/>
    <input type="hidden" name="tb_auth_tag.student_auth" id="_student_auth" value="${item.student_auth}"/>
    <input type="hidden" name="tb_auth_tag.parent_auth" id="_parent_auth" value="${item.parent_auth}"/>
    <input type="hidden" name="tb_auth_tag.version" id="_version" value="${item.version}"/>
    <input type="hidden" name="tb_auth_tag.id" value="${id}"/>
    <!-- 表单 -->
    <div class="form-group" style="display:block">
        <label class="col-sm-1 control-label no-padding-right"> 权限名称 </label>
        <div class="col-sm-10">
            <input type="text" id="_name" name="tb_auth_tag.name" required="required" value="${item.name}" placeholder="暂无权限名称" class="form-control"/>
        </div>
    </div>

    <div class="form-group" style="display:block">
        <label class="col-sm-1 control-label no-padding-right"> 权限范围 </label>
        <div class="col-sm-10">


            <!-- 配置grid row这个class不能省略，会出问题-->
            <div class="row">
                <div class="col-sm-4">
                    <div class="col-sm-12">
                        <div class="widget-box widget-color-blue2">
                            <div class="widget-header">
                                <h4 class="widget-title lighter smaller">老师</h4>
                            </div>

                            <div class="widget-body">
                                <div class="widget-main padding-8">
                                    <ul id="zTree" class="ztree"></ul>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="col-sm-4">
                    <div class="col-sm-12">
                        <div class="widget-box widget-color-blue2">
                            <div class="widget-header">
                                <h4 class="widget-title lighter smaller">家长</h4>
                            </div>

                            <div class="widget-body">
                                <div class="widget-main padding-8">
                                    <ul id="zTree2" class="ztree"></ul>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="col-sm-4">
                    <div class="col-sm-12">
                        <div class="widget-box widget-color-blue2">
                            <div class="widget-header">
                                <h4 class="widget-title lighter smaller">学生</h4>
                            </div>

                            <div class="widget-body">
                                <div class="widget-main padding-8">
                                    <ul id="zTree3" class="ztree"></ul>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="clear"></div>
            </div>

        </div>
    </div>
    <!-- 按钮 -->
    @ include("/common/_curd/_btn.html", {btn_save:"新增", btn_close:"关闭"}) {}
</form>

<script>
    $(document).ready(function(){
        initZtree("getStuTree","zTree3",$("#_student_auth").val());
        initZtree("getParTree","zTree2",$("#_parent_auth").val());
        initZtree("getTeaTree","zTree",$("#_teacher_auth").val());
    });

    function saveBefore() {
        var zTree = $.fn.zTree.getZTreeObj("zTree");
        var zTree2 = $.fn.zTree.getZTreeObj("zTree2");
        var zTree3 = $.fn.zTree.getZTreeObj("zTree3");

        var tree = selectTeaNodes(zTree);
        var tree2 = selectNodes(zTree2);
        var tree3 = selectNodes(zTree3);
        
        console.log(tree);
        console.log(tree2);
        console.log(tree3);


        $("#_teacher_auth").val(tree);
        $("#_parent_auth").val(tree2);
        $("#_student_auth").val(tree3);
        

        if (!tree && !tree2 && !tree3) {
            layer.msg("选项不能为空", {shift: 6});
            return false;
        }
        return true;
    }

    function selectTeaNodes(tree){
        var nodes = tree.getCheckedNodes();
        var ids = "";
        var names = "";
        for (var i=0, l=nodes.length; i<l; i++) {
            if (nodes[i].leaf){
                ids += "," + nodes[i].id;
            }
        }
        ids = ids.substring(1);
        return ids;
    }

    function selectNodes(tree){
        var nodes = tree.getCheckedNodes();
        var ids = "";
        var names = "";
        for (var i=0, l=nodes.length; i<l; i++) {
            //忽略老师或学生的顶级节点
            if (nodes[i].id == 1002 || nodes[i].id == 1001){
                continue;
            }
            ids += "," + nodes[i].id;
        }
        ids = ids.substring(1);
        return ids;
    }


    var setting = {
        check: {
            enable: true,
            chkboxType: {"Y": "ps", "N": "ps"},
            chkStyle : "checkbox"
        },
        data: {
            simpleData: {
                enable: true
            }
        }
    };

    function initZtree(url,node,val) {

        $.post("${basePath}/${code}/"+url,{'val':val},function(data){
            if(data.code===0){
                $.fn.zTree.init($("#"+node), setting, data.data);
            }
            else{
                layer_alert("数据加载失败", "error");
            }
        }, "json");
    }

</script>

<!-- 引入所需js -->
<link rel="stylesheet" href="${basePath}/static/zTree/css/zTreeStyle/zTreeStyle.css" />
<script src="${basePath}/static/zTree/js/jquery.ztree.core.js" type="text/javascript" ></script>
<script src="${basePath}/static/zTree/js/jquery.ztree.excheck.js" type="text/javascript"></script>
@}
